package com.qf.ebook.controller;

import org.apache.commons.io.IOUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;

@RestController
public class DownloadController {


    @GetMapping("/download")
    public void download(String pic, HttpServletResponse resp) throws IOException {
        //1. 获取图片路径
        String picPath = "D:\\pic\\" + pic;

        //2. 获取图片的字节流InputStream
        FileInputStream inputStream = new FileInputStream(picPath);

        //3. 获取响应的OutputStream
        ServletOutputStream outputStream = resp.getOutputStream();

        //4. 设置响应头以附件的形式打开
        resp.setHeader("Content-Disposition", "attachment;filename="+pic);

        //5. 通过IOUtils写回,已经是响应数据了
        IOUtils.copy(inputStream,outputStream);
    }

}
